package com.example.practice.utils;

import android.util.Log;

import com.google.gson.JsonObject;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class OkHttpUtils {
    public static MediaType JSON= MediaType.parse("application/json;charset=utf-8");
    public static void sendOkHttp(boolean isGet, boolean localhost, String action, JsonObject jsonObject, Callback callback){
        OkHttpClient client=new OkHttpClient();
        Request.Builder builder = new Request.Builder();
        String url = null;
        if (localhost){
            url="http://106.55.142.207:81/"+action;
        }
//        if(userToken.token!=null)
//        {
//            builder.addHeader("Authorization",userToken.token);
//        }
//        Log.d("OkHttp", "sendOkHttp: "+url);
        Request request;
        if (isGet){
//            Log.d("OkHttp", "sendgetOkHttp: "+url);
            request=builder.get().url(url).build();
        }else {
//            Log.d("OkHttp", "sendpostOkHttp: "+url);
            RequestBody body= RequestBody.create(JSON,String.valueOf(jsonObject));
            request=builder.post(body).url(url).build();
        }
        Call call = client.newCall(request);
        call.enqueue(callback);

    }
}
